package br.com.dreamsource.mobile.components;

import br.com.dreamsource.mobile.jmesql.exceptions.SQLException;
import br.com.dreamsource.mobile.jmesql.jdbcResultSet;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Elizeu Nogueira da Rosa Jr.
 * @version 0.1
 * @date 22.05.2007
 *
 */

public class DBChoiceGroup extends ChoiceGroup {
    
    private Vector ids = new Vector(1);

    public DBChoiceGroup(String label, int choiceType, String[] stringElements, Image[] imageElements) {
        super(label, choiceType, stringElements, imageElements);
    }
    
    public void loadByResultSet(jdbcResultSet resultSet, String showField, String indexField) throws SQLException {
        while (resultSet.next()) {
            this.append(resultSet.getString(indexField), resultSet.getString(showField), null);
        }
    }
    
    public void append(String id, String stringPart, Image imagePart) {
        this.append(stringPart, imagePart);
        ids.addElement(id);
    }
    
    public void setSelectedIndexByID(String id) {
        Enumeration en = this.ids.elements();
        while (en.hasMoreElements()) {
            String vectorId = (String)en.nextElement();
            if (vectorId.equals(id)) {
                this.setSelectedIndex(this.ids.indexOf(vectorId), true);
            }
        }
    }
    
    public String getSelectedItemID() {
        if (this.getSelectedIndex() > -1) {
            return (String)ids.elementAt(this.getSelectedIndex());
        } else {
            return "0";
        }
    }
    
}
